package com.feiyu.shopsAndgoods.controller;

import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.fastjson.JSON;
import com.feiyu.common.entity.AddressEntity;
import com.feiyu.shopsAndgoods.entity.CheckShopEntity;
import com.feiyu.shopsAndgoods.entity.DictGoodsEntity;
import com.feiyu.shopsAndgoods.entity.GoodsEntity;
import com.feiyu.shopsAndgoods.service.CheckShopService;
import com.feiyu.shopsAndgoods.service.DictGoodsService;
import com.feiyu.shopsAndgoods.service.GoodsService;
import com.feiyu.shopsAndgoods.service.openFeign.CommonFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.web.bind.annotation.*;

import com.feiyu.shopsAndgoods.entity.ShopEntity;
import com.feiyu.shopsAndgoods.service.ShopService;
import com.feiyu.common.utils.PageUtils;
import com.feiyu.common.utils.R;


/**
 * @author wanghanbin
 * @email 1336891708@qq.com
 * @date 2021-04-03 12:36:05
 */
@RestController
public class ShopController {
    @Autowired
    private ShopService shopService;

    @Autowired
    private CommonFeignService commonFeignService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private DictGoodsService dictGoodsService;

    @Autowired
    private CheckShopService checkShopService;

    /**
     * 商城搜索店铺名模糊查找店铺
     */
    @RequestMapping("/openFeiyu/searchShopsByShopName")
    //@RequiresPermissions("product:shop:list")
    public R searchShopsByShopName(@RequestBody Map<String, Object> params) {
        PageUtils page = shopService.queryPageByShopName(params);
        if (page.getList().size() > 0) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("totalCount", page.getTotalCount());
            hashMap.put("pageSize", page.getPageSize());
            hashMap.put("totalPage", page.getTotalPage());
            hashMap.put("currPage", page.getCurrPage());
            List<?> pageList = page.getList();
            List<Map<String, Object>> shopInfo_list = new ArrayList<>();
            for (int i = 0; i < pageList.size(); i++) {
                HashMap<String, Object> shopInfo = new HashMap<>();
                ShopEntity shopEntity = (ShopEntity) pageList.get(i);
                AddressEntity addressEntity = new AddressEntity();
                addressEntity.setAddressId(shopEntity.getShopAddress());
                R address = commonFeignService.searchAddressById(addressEntity);
                Object addressInfo_data = address.get("data");
                Map addressInfo_data_map = (Map) JSON.toJSON(addressInfo_data);
                shopInfo.put("shopId", shopEntity.getShopId());
                shopInfo.put("userId", shopEntity.getUserId());
                shopInfo.put("shopName", shopEntity.getShopName());
                shopInfo.put("shopAvator", shopEntity.getShopAvator());
                shopInfo.put("shopLicense", shopEntity.getShopLicense());
                shopInfo.put("shopIntro", shopEntity.getShopIntro());
                shopInfo.put("shopMain", shopEntity.getShopMain());
                shopInfo.put("shopType", shopEntity.getShopType());
                shopInfo.put("shopAddress", addressInfo_data_map.get("recipientAdd"));
                shopInfo.put("shopState", shopEntity.getShopState());
                shopInfo_list.add(shopInfo);
            }
            hashMap.put("list", shopInfo_list);
            return R.ok("操作成功").put("data", hashMap);
        } else if (page.getList().size() == 0) {
            return R.error("为查询到相关店铺").put("data", null);
        } else {
            return R.error("操作失败").put("data", null);
        }
    }


    /**
     * 商城精确查找店铺
     */
    //https://test-wanghanbin.oss-cn-hangzhou.aliyuncs.com/feiyuImages/
    @RequestMapping("/openFeiyu/searchShopsByShopId")
    //@RequiresPermissions("product:shop:info")
    public R searchShopsByShopId(@RequestBody ShopEntity shopEntity) {
        ShopEntity shop = shopService.getById(shopEntity.getShopId());
        if (shop == null) {
            return R.error("未查询到该店铺").put("data", null);
        }
        AddressEntity addressEntity = new AddressEntity();
        addressEntity.setAddressId(shop.getShopAddress());
        R addressInfo = commonFeignService.searchAddressById(addressEntity);
        Object addressInfo_data = addressInfo.get("data");
        Map addressInfo_data_map = (Map) JSON.toJSON(addressInfo_data);
        List<GoodsEntity> goods = goodsService.getAllByShopId(shop.getShopId());
        HashMap<String, Object> shopInfo = new HashMap<>();
        shopInfo.put("shopId", shop.getShopId());
        shopInfo.put("userId", shop.getUserId());
        shopInfo.put("shopName", shop.getShopName());
        shopInfo.put("shopAvator", shop.getShopAvator());
        shopInfo.put("shopLicense", shop.getShopLicense());
        shopInfo.put("shopIntro", shop.getShopIntro());
        shopInfo.put("shopMain", shop.getShopMain());
        shopInfo.put("shopType", shop.getShopType());
        shopInfo.put("shopAddress", addressInfo_data_map.get("recipientAdd"));
        shopInfo.put("shopState", shop.getShopState());
        List<HashMap<String, Object>> goodInfo_list = new ArrayList<>();
        for (GoodsEntity good : goods) {
            HashMap<String, Object> goodInfo = new HashMap<>();
            HashMap<String, Object> goodInfoImages = new HashMap<>();
            String goodsImages = good.getGoodsImages();
            String[] split = goodsImages.split(";");
            Integer goodsTagId = good.getGoodsTagId();
            DictGoodsEntity tag = dictGoodsService.getById(goodsTagId);
            String url = "https://test-wanghanbin.oss-cn-hangzhou.aliyuncs.com/feiyuImages/";
            for (int i = 0; i < split.length; i++) {
                goodInfoImages.put("goodImage_" + (i + 1), url + split[i]);
            }
            goodInfo.put("goodsId", good.getGoodsId());
            goodInfo.put("goodsName", good.getGoodsName());
            goodInfo.put("goodsIntro", good.getGoodsIntro());
            goodInfo.put("goodsImages", goodInfoImages);
            goodInfo.put("goodsLicense", good.getGoodsLicense());
            goodInfo.put("goodsRent", good.getGoodsRent());
            goodInfo.put("goodsDeposit", good.getGoodsDeposit());
            goodInfo.put("goodsPrice", good.getGoodsPrice());
            goodInfo.put("goodsTagName", tag.getDictGoodsLable());
            goodInfo.put("goodsTimeMode", good.getGoodsTimeMode());
            goodInfo.put("isAllowedBuy", good.getIsAllowedBuy());
            goodInfo.put("goodsPostageMode", good.getGoodsPostageMode());
            goodInfo.put("goodsMaxRenewalTime", good.getGoodsMaxRenewalTime());
            goodInfo.put("goodsNums", good.getGoodsNums());
            goodInfo.put("goodsState", good.getGoodsState());
            goodInfo_list.add(goodInfo);
        }
        shopInfo.put("list", goodInfo_list);
        return R.ok("操作成功").put("data", shopInfo);
    }

    /**
     * 店铺后台精确查找店铺
     */
    @PostMapping("/searchShopsByShopIdByShop")
    public R searchShopsByShopIdByShop(@RequestBody ShopEntity shopEntity) {
        ShopEntity shop = shopService.getById(shopEntity.getShopId());
        if (shop == null) {
            return R.error("未查询到该店铺").put("data", null);
        }
        HashMap<String, Object> map = new HashMap<>();
        BeanMap beanMap = BeanMap.create(shop);
        for (Object key : beanMap.keySet()) {
            map.put(key.toString(), beanMap.get(key));
        }
        AddressEntity addressEntity = new AddressEntity();
        addressEntity.setAddressId(shop.getShopAddress());
        R address = commonFeignService.searchAddressById(addressEntity);
        Object address_data = address.get("data");
        Map address_data_map = (Map) JSON.toJSON(address_data);
        map.remove("delFlag");
        map.remove("money");
        map.put("shopAddress", address_data_map.get("recipientAdd"));
        return R.ok("操作成功").put("data", map);
    }

    /**
     * 店铺后台修改店铺信息
     */
    @RequestMapping("/updateShopByShopId")
    //@RequiresPermissions("product:shop:update")
    public R updateShopByShopId(@RequestBody CheckShopEntity checkShopEntity) {
        checkShopService.save(checkShopEntity);
        return R.ok("操作成功，信息正在审核中").put("data", null);
    }

    /**
     * 店铺后台删除店铺
     */
    @RequestMapping("/deleteShopByShopId")
    public R deleteShopByShopId(@RequestBody ShopEntity shopEntity) {
        shopService.removeById(shopEntity.getShopId());
        return R.ok("删除成功").put("data", null);
    }

    /**
     * 店铺后台精确查询店铺审核状态
     */
    @PostMapping("/getReviewShopByCheckShopId")
    public R getReviewShopByCheckShopId(@RequestBody CheckShopEntity checkShopEntity) {
        CheckShopEntity checkShopInfo = checkShopService.getById(checkShopEntity.getCheckShopId());
        if (checkShopInfo == null) {
            return R.error("未查询到该店铺审核信息").put("data", null);
        }
        HashMap<String, Object> map = new HashMap<>();
        BeanMap beanMap = BeanMap.create(checkShopInfo);
        for (Object key : beanMap.keySet()) {
            map.put(key.toString(), beanMap.get(key));
        }
        AddressEntity addressEntity = new AddressEntity();
        addressEntity.setAddressId(checkShopInfo.getShopAddress());
        R address = commonFeignService.searchAddressById(addressEntity);
        Object address_data = address.get("data");
        Map address_data_map = (Map) JSON.toJSON(address_data);
        map.remove("delFlag");
        map.put("shopAddress", address_data_map.get("recipientAdd"));
        return R.ok("操作成功").put("data", map);
    }

    /**
     * 店铺后台查看所有店铺审核状态
     */
    @PostMapping("/getAllReviewShopByShopId")
    public R getAllReviewShopByShopId(@RequestBody Map<String, Object> params) {
        PageUtils checkShopInfos = checkShopService.queryPageByShopId(params);
        List<?> list = checkShopInfos.getList();
        if (list.size() > 0) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("totalCount", checkShopInfos.getTotalCount());
            hashMap.put("pageSize", checkShopInfos.getPageSize());
            hashMap.put("totalPage", checkShopInfos.getTotalPage());
            hashMap.put("currPage", checkShopInfos.getCurrPage());
            List<Map<String, Object>> checkShopInfos_list = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                HashMap<String, Object> checkShopInfo_map = new HashMap<>();
                CheckShopEntity checkShopInfo = (CheckShopEntity) list.get(i);
                AddressEntity addressEntity = new AddressEntity();
                if (checkShopInfo.getShopAddress() == null) {
                    return R.error("操作失败").put("data", null);
                }
                addressEntity.setAddressId(checkShopInfo.getShopAddress());
                R address = commonFeignService.searchAddressById(addressEntity);
                Object address_data = address.get("data");
                Map address_data_map = (Map) JSON.toJSON(address_data);
                checkShopInfo_map.put("checkShopId", checkShopInfo.getCheckShopId());
                checkShopInfo_map.put("shopId", checkShopInfo.getShopId());
                checkShopInfo_map.put("shopName", checkShopInfo.getShopName());
                checkShopInfo_map.put("shopAvator", checkShopInfo.getShopAvator());
                checkShopInfo_map.put("shopLicense", checkShopInfo.getShopLicense());
                checkShopInfo_map.put("shopIntro", checkShopInfo.getShopIntro());
                checkShopInfo_map.put("shopMain", checkShopInfo.getShopMain());
                checkShopInfo_map.put("shopType", checkShopInfo.getShopType());
                checkShopInfo_map.put("shopAddress", address_data_map.get("recipientAdd"));
                checkShopInfo_map.put("checkShopApplyTime", checkShopInfo.getCheckShopApplyTime());
                checkShopInfo_map.put("checkStats", checkShopInfo.getCheckState());
                checkShopInfo_map.put("checkUserId", checkShopInfo.getCheckUserId());
                checkShopInfo_map.put("checkTime", checkShopInfo.getCheckTime());
                checkShopInfo_map.put("checkRe", checkShopInfo.getCheckRe());
                checkShopInfos_list.add(checkShopInfo_map);
            }
            hashMap.put("list", checkShopInfos_list);
            return R.ok().put("data", hashMap);
        } else if (list.size() == 0) {
            return R.error("未查询到相关店铺的审核状态信息").put("data", null);
        } else {
            return R.error("操作失败").put("data", null);
        }
    }
}
